package com.jiaoyou.baojian.http;

import android.util.Log;

import com.jiaoyou.baojian.BaojianApplication;
import com.jiaoyou.baojian.utils.ACache;

import java.io.IOException;
import java.util.Iterator;
import java.util.Set;

import okhttp3.Cookie;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Response;
import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func1;

/**
 * Created by DemonZYM on 17/5/5.
 */

public class ReceivedCookiesInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
        Response originalResponse = chain.proceed(chain.request());
        //这里获取请求返回的cookie
        if (!originalResponse.headers("Set-Cookie").isEmpty()) {
            Iterator<String> ite = originalResponse.headers().values("Set-Cookie").iterator();
            while(ite.hasNext()){
                String value = ite.next();
                if(value.contains("remember-me")){
                    Cookie cookie = Cookie.parse(HttpUrl.parse(Api.BASE_URL), value);
                    ACache aCache = ACache.get(BaojianApplication.getInstance());
                    aCache.put("mobile_token", cookie.value());
                }
            }

        }

        return originalResponse;
    }
}
